package com.breaktian.assemble.refresh.superrefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.breaktian.assemble.R;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.assemble.utils.ThreadUtil;

/**
 * Created by tiankeke on 2017/6/20.
 * 通用refreshlayout
 */

public class PullRefreshLayout extends PullDownRefreshLayout {
    private Context mContext;
    private ImageView footerImageView;
    private TextView footerTextView;
    private ProgressBar footerProgressBar;
    private OnRefreshListener mRefreshListener;

    public PullRefreshLayout(Context context) {
        super(context);
        init(context);
    }

    public PullRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setFooterView(createFooterView());

        setOnPushLoadMoreListener(new OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                LogUtil.d("onLoadMore");
                footerTextView.setText("正在加载...");
                footerImageView.setVisibility(View.GONE);
                footerProgressBar.setVisibility(View.VISIBLE);
                if(mRefreshListener!=null){
                    mRefreshListener.onLoadMore();
                }
            }

            @Override
            public void onPushDistance(int distance) {
                LogUtil.d("onPushDistance");
            }

            @Override
            public void onPushEnable(boolean enable) {
                LogUtil.d("onPushEnable :"+enable);
                footerTextView.setText(enable ? "松开加载" : "上拉加载");
                footerImageView.setVisibility(View.VISIBLE);
                footerImageView.setRotation(enable ? 0 : 180);
            }
        });


    }

    /**
     * 设置刷新监听器
     * */
    public void setOnRefreshListener(OnRefreshListener refreshListener){
        this.mRefreshListener = refreshListener;
        setOnPulldownRefreshListener(new OnPullDownRefreshListener() {
            @Override
            public void onPullDown() {
                if(mRefreshListener!=null){
                    mRefreshListener.onPullDown();
                }
            }
        });
    }

    public interface OnRefreshListener{
        void onPullDown();
        void onLoadMore();
    }

    /**
     * 结束刷新
     * */
    public void stopLoadMore(){
        ThreadUtil.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                footerImageView.setVisibility(View.VISIBLE);
                footerProgressBar.setVisibility(View.GONE);
                setLoadMore(false);
            }
        });
    }


    View createFooterView(){
        View footerView = LayoutInflater.from(mContext).inflate(R.layout.bm_refresh_layout_footer, null);
        footerProgressBar = (ProgressBar)footerView.findViewById(R.id.footer_pb_view);
        footerTextView = (TextView)footerView.findViewById(R.id.footer_text_view);
        footerImageView = (ImageView)footerView.findViewById(R.id.footer_image_view);
        return footerView;
    }




}
